package com.itheima.demo;

import org.activiti.engine.*;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

/**
 * 创建ProcessEngineConfiguration，通过ProcessEngineConfiguration创建ProcessEngine，
 * 在创建ProcessEngine的同时会自动创建数据库。
 */
public class ActivitiTest {

    /**
     * 1、运行以下程序即可完成Activiti数据库表的创建，通过改变activiti-cfg.xml中的databaseSchemaUpdate参数的值执行不同的数据表处理策略。
     *
     * 2、上面的方法createProcessEngineConfigurationFromResource在执行activiti-cfg.xml中找固定的名称processEngineConfiguration，
     * 也可以使用重载方法调用，这时就可以不用限定processEngineConfiguration名称。
     */
    public static void main(String[] args) {
//        test();
        testService();
    }

    private static void test() {
        //创建ProcessEngineConfiguration对象
//        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti-cfg.xml");
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti-cfg2.xml");
        //创建ProcessEngine对象
        ProcessEngine processEngine = configuration.buildProcessEngine();
        System.out.println("processEngine = " + processEngine);
    }

    private static void testSimple() {
        //该方法有限制
        //①Activiti的配置文件名必须为activiti.cfg.xml
        //②Bean的id必须是processEngineConfiguration
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        System.out.println("processEngine = " + processEngine);
    }

    private static void testService() {
        //创建ProcessEngineConfiguration对象
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti-cfg2.xml");
        //创建ProcessEngine对象
        ProcessEngine processEngine = configuration.buildProcessEngine();

        RepositoryService repositoryService = processEngine.getRepositoryService();
        RuntimeService runtimeService = processEngine.getRuntimeService();
        TaskService taskService = processEngine.getTaskService();
        HistoryService historyService = processEngine.getHistoryService();
        ManagementService managementService = processEngine.getManagementService();

        System.out.println("repositoryService = " + repositoryService);
        System.out.println("runtimeService = " + runtimeService);
        System.out.println("taskService = " + taskService);
        System.out.println("historyService = " + historyService);
        System.out.println("managementService = " + managementService);
    }

    //流程部署
    @Test
    public void deployment() {
        //获取ProcessEngine对象 默认配置文件名称：activiti.cfg.xml 并且configuration的Bean实例ID为processEngineConfiguration
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //获取RepositoryService对象进行流程部署
        RepositoryService repositoryService = processEngine.getRepositoryService();
        //进行部署，将对应的流程定义文件生成到数据库当中，作为记录进行保存
        Deployment deployment = repositoryService.createDeployment()
                .addClasspathResource("holiday/holiday.bpmn")     //加载流程文件
                .addClasspathResource("holiday/holiday.png")
                .name("请假流程")       //设置流程名称
                .key("holidayKey")
                .deploy();             //部署

        //输出部署信息
        System.out.println("流程名称：" + deployment.getName());
        System.out.println("流程ID：" + deployment.getId());
        System.out.println("流程Key：" + deployment.getKey());
    }

    //启动一个流程实例
    public void getInstance() {
        //获取ProcessEngine对象，默认配置文件名称：activiti.cfg.xml 并且configuration的Bean实例ID为processEngineConfiguration
        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
        //获取Runtime
        RuntimeService runtimeService = processEngine.getRuntimeService();
        //定义一个map集合存放assignee值
        Map<String, Object> map = new HashMap<>();
        map.put("assignee01", "zhangsan");
        map.put("assignee02", "lisi");
        map.put("assignee03", "wangwu");
        map.put("assignee04", "zhaoliu");
        //启动一个流程实例
        ProcessInstance holiday = runtimeService.startProcessInstanceByKey("holidayKey", map);
        System.out.println("流程实例name：" + holiday.getName());
    }
}
